import { Module } from 'vuex'
import { getResource } from '@/api/resource'
import { goodsTypeList } from '@/api/lose/goodsType'

export interface GoodsTypeState {
  loseGoodsClassify: {
    key: string;
    name: string;
  }[],
  typeList: {
    goodsType: string,
    typeId: string
  }[]
}

// eslint-disable-next-line
const goodsType: Module<GoodsTypeState, any> = {
  namespaced: true,
  state: () => ({
    loseGoodsClassify: [],
    typeList: []
  }),
  getters: {
    getName (state) {
      console.log('state.loseGoodsClassify',state.loseGoodsClassify)
      return (key: string) => state.loseGoodsClassify.find(item => item.key === key)?.name
    },
    getGoodsTypeName (state) {
      return (key: string) => state.typeList.find(item => item.typeId === key)?.goodsType
    }
  },
  mutations: {
    SET_LOSE_GOODS_CLASSIFY(state, payload) {
      state.loseGoodsClassify = payload
    },
    SET_GOODS_TYPE(state, payload) {
      state.typeList = payload
    }
  },
  actions: {
    getDict({ commit }) {
      return new Promise((resolve, reject) => {
        getResource("lose_goods_classify").then(res => {
          if (res && res.data) {
            let list = res.data?.properties?.meta || []
            commit('SET_LOSE_GOODS_CLASSIFY', list)
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(reject)
      })
    },
    getGoodsType({ commit }, data) {
      return new Promise((resolve, reject) => {
        goodsTypeList({password:'',userName:''}).then((res: any) => {
          if (res?.code === '1') {
            if (res && res.data) {
              // const { list } = res.data
              commit('SET_GOODS_TYPE', res.data)
              resolve(res)
            }
          } else {
            reject(res)
          }
        }).catch(reject)
      })
    }
  }
}

export default goodsType
